package com.yuntu.service;

import com.yuntu.tools.PageUtil;
import com.yuntu.dao.PeitemsMapper;
import com.yuntu.pojo.Peitems;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class PeitemsServiceImpl implements PeitemsService{
    @Autowired
    private PeitemsMapper peitemsMapper;
    //查询
    @Override
    public List<Peitems> peitemsAll() throws Exception {
        List<Peitems> peitemsList=peitemsMapper.getPeitemsList();
        return peitemsList;
    }
    //添加
    @Override
    public boolean add(Peitems peitems) throws Exception {
        boolean flag=false;
        int count=peitemsMapper.addPeitemsType(peitems);
        if (count>0){
            flag=true;
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败!");
        }
        return flag;
    }
    //删除
    @Override
    public boolean del(int itemid) throws Exception {
        boolean flag=false;
        int count=peitemsMapper.deletePeitemsById(itemid);
        if (count > 0){
            flag = true;
            System.out.println("删除成功!");
        }else {
            System.out.println("删除失败!");
        }
        return flag;
    }

    //根据id查询
    @Override
    public Peitems getitemid(int itemid) throws Exception {
        Peitems peitems=null;
        peitems = peitemsMapper.getPeitemsById(itemid);
        System.err.println("项目名："+peitems.getItemname());
        return peitems;
    }
    //修改
    @Override
    public boolean upd(Peitems peitems) throws Exception {
        boolean flag=false;
        int count=peitemsMapper.updatePeitems(peitems);
        if (count > 0){
            flag = true;
            System.out.println("修改成功!");
        }else {
            System.out.println("修改失败!");
        }
        return flag;
    }
    //分页
    @Override
    public List<Peitems> getPageUtil(PageUtil<Peitems> pageUtil) throws Exception {
        List<Peitems> peitemsList=new ArrayList<>();
        int count=peitemsMapper.getCount();
        pageUtil.setCounts(count);
        if (count>0){
            if (pageUtil.getPageindex()>pageUtil.getPagecount()){
                pageUtil.setPageindex(pageUtil.getPagecount());
            }
            peitemsList=peitemsMapper.getPageAll(pageUtil);
            pageUtil.setLists(peitemsList);
        }
        return peitemsList;
    }

    @Override
    public Peitems exist(String itemname) throws Exception {
        Peitems peitems=peitemsMapper.getExits(itemname);
        return peitems;
    }

    @Override
    public Peitems login(String itemname,String ref) throws Exception {
        Peitems peitems=peitemsMapper.getLoginPe(itemname);
        if (null!=peitems){
            if(!peitems.getRef().equals(ref)){
                peitems=null;
            }
        }
        return peitems;
    }

}
